<?php

namespace App\Service;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Category extends Model
{
    //
    const DISPLAY = 1;
    const DISPLAY_NONE = 2;
    public static function getStatusList(){
        return [
            self::DISPLAY   =>  '展示',
            self::DISPLAY_NONE  =>  '不展示'
        ];
    }

    /**
     * @param $status
     *
     * @return mixed
     */
    public function getStatusString($status){
        $statusList =  $this->getStatusList();
        return $statusList[$status];
    }


    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        $this->request = $request;
        $model = $this->where('pid',21);
        if(isset($request->status)&&$request->status){
            $model = $model->where('status',$request->status);
        }
        //状态
        $model=$model->paginate($this->request->limit)
            ->toArray();
        return $model;
    }


    public static function getDisplay($length=4){
        $res = self::where('status',self::DISPLAY)->limit(4)->get();
        if($res){
            return $res->toArray();
        }else{
            return [];
        }
    }
    /**
     * @param $value
     *
     * @return mixed
     */
    public function getStatusAttribute($value){
        $config= $this->getStatusList();
        return $config[$value];
    }

    public function draft($id = []){
        if($id == []){
            $this->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);
            }else{
                $this->where('id',$id)->where('status',self::DISPLAY)->update(['status'=>self::DISPLAY_NONE]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function published($id = []){
        if($id == []){
            $this->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);
            }else{
                $this->where('id',$id)->where('status',self::DISPLAY_NONE)->update(['status'=>self::DISPLAY]);

            }
        }
    }

    public function sortCol($id = [],$sort = 50){
        if($id == []){
        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);
            }else{
                $this->where('id',$id)->where('sort','<>',$sort)->update(['sort'=>$sort]);

            }
        }
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->delete();
            }else{
                $this->where('id',$id)->delete();

            }
        }
    }


    /**
     * @param $post
     */
    public function insertOrUpdate($post){
        if(isset($post['id'])){
            $old = self::find($post['id']);
            $old->cat_name = $post['cat_name'];
            $old->slug = $post['slug']?$post['slug']:'';
            if(isset($post['status'])){
                $old->status = $post['status'];
            }
            $old->sort = $post['sort']?$post['sort']:50;
            $old->pid = 21;
            $old->save();
        }else{
            $this->cat_name = $post['cat_name'];
            $this->slug = $post['slug'];

            $this->status = isset($post['status'])?$post['status']:self::DISPLAY;
            $this->sort = $post['sort']?$post['sort']:50;
            $this->pid = 21;
            $this->save();
        }
    }

}
